import { getURLObj } from '@/utils/common';
import { NextRequest, NextResponse } from 'next/server';

async function redirct(req: NextRequest) {
  const searchParams = req.nextUrl.searchParams;

  const backurl =
    searchParams.get('backurl') || getURLObj('/', req.url).toString();
  const delayMs = Number(searchParams.get('delay') || '0');

  if (delayMs > 0) {
    await new Promise(resolve => setTimeout(resolve, delayMs));
  }

  return NextResponse.redirect(backurl);
}

export async function GET(req: NextRequest) {
  return await redirct(req);
}
